package com.platform.common.dataImport;

import com.platform.common.exception.BusinessException;
import com.platform.common.utils.dataImport.BaseImportExcel;
import com.platform.modules.mall.entity.MallBrandEntity;
import com.platform.modules.mall.entity.MallShopsEntity;
import com.platform.modules.mall.service.MallBrandService;
import com.platform.modules.mall.service.MallShopsService;
import com.platform.modules.mall.service.impl.MallShopsServiceImpl;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class ShopsImport extends BaseImportExcel {

    {
        comparison = "店铺名称,联系电话,详细位置,经纬度,简介,店铺详情,人均消费金额,店铺评分,商圈名称,品牌名称,营业时间";
    }

    List<MallShopsEntity> entities = new ArrayList<>();
    Map<String,Integer> brandCount = new HashMap<>();
    private MallShopsService shopsService;
    private MallBrandService brandService;

    public ShopsImport(MallShopsService shopsService, MallBrandService brandService){
        this.shopsService = shopsService;
        this.brandService = brandService;

    }

    @Override
    public void major(String[] strs) {

        // 判断店铺是否存在
        if (shopsService.lambdaQuery().select(MallShopsEntity::getId).eq(MallShopsEntity::getName,getValue(strs,0)).count() > 0) {
            throw new BusinessException("店铺信息已存在！");
        }

        // 获取商圈信息
        String tradeAreaId = getValue(strs,8);
        if(tradeAreaId != null){
            try {
                tradeAreaId = shopsService.lambdaQuery().select(MallShopsEntity::getId).like(MallShopsEntity::getName,tradeAreaId).one().getId();
            }catch (Exception e){
                e.printStackTrace();
                throw new BusinessException("商圈信息获取失败！");
            }
        }
        // 获取品牌信息
        String brandId = getValue(strs,9);
        if(brandId != null){
            try {
                brandId = brandService.lambdaQuery().select(MallBrandEntity::getId).like(MallBrandEntity::getName,brandId).one().getId();
                brandCount.put(brandId,Optional.ofNullable(brandCount.get(brandId)).orElse(0) + 1);
            }catch (Exception e){
                e.printStackTrace();
                throw new BusinessException("品牌信息获取失败！");
            }
        }
        // 设置店铺基础信息
        MallShopsEntity entity = new MallShopsEntity();
        entity.setName(getValue(strs,0));
        entity.setTelephone(getValue(strs,1));
        entity.setDetails(getValue(strs,2));
        entity.setLocation(getValue(strs,3));
        entity.setIntroduction(getValue(strs,4));
        entity.setShopDesc(getValue(strs,5));
        entity.setAveragePrice(Integer.parseInt(Optional.ofNullable(getValue(strs,6)).orElse("9")));
        entity.setShopScore(Float.parseFloat(Optional.ofNullable(getValue(strs,7)).orElse("4.5")));
        entity.setTradeAreaId(tradeAreaId);
        entity.setBrandId(brandId);
        entity.setWorkTime(getValue(strs,10));
        // 设置基础固定信息
        entity.setIsOnSale(1);
        entity.setType(1);
        entity.setLevel(2);
        entity.setSort(1);
        entity.setHot(0);
        entity.setDeleteStatus(1);
        entity.setCreateTime(new Date());
        entities.add(entity);

        ((MallShopsServiceImpl)shopsService).shopDataChange(entity,1);
    }

    @Override
    public void after() {
        if(entities.isEmpty()){
            throw new BusinessException("可导入的数据为0");
        }

        for(String key : brandCount.keySet()){
            // 更新匹配下门店数量
            brandService.lambdaUpdate().setSql("shop_count = shop_count + "+brandCount.get(key)).eq(MallBrandEntity::getId,key).update();
        }

        boolean result = shopsService.saveBatch(entities);
        if (!result) {
            throw new BusinessException("导入失败，请检查数据或联系管理员");
        }
        super.after();
    }
}
